package com.eshore.ezone.activities;

import com.eshore.ezone.R;
import com.eshore.ezone.utils.Preferences;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class TrafficTipsActivity extends Activity {
    private ImageView mBackground;
    private SetBackgroundTask mSetBackgroundTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        checkTipsShown();
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.traffic_tips);
        setupViews();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        onCancelTask();
    }

    private void onCancelTask() {
        final SetBackgroundTask task = mSetBackgroundTask;
        if(task!=null && task.getStatus() == AsyncTask.Status.RUNNING) {
            task.cancel(true);
            mSetBackgroundTask = null;
        }
    }

    private void checkTipsShown() {
        final SharedPreferences prfs = PreferenceManager.getDefaultSharedPreferences(this);
        final boolean showTips =  prfs.getBoolean(Preferences.PREFERENCE_TRAFFIC_TIPS, true);
        if(!showTips) {
            startSplashActivity();
        }
    }
    
    private void startSplashActivity() {
        final Intent intent = new Intent(TrafficTipsActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
    
    private void setupViews() {
        final CheckBox cb = (CheckBox) findViewById(R.id.cb_traffic_tips);
        cb.setOnCheckedChangeListener(new CheckedChangeListener());
        final Button agreeBtn = (Button) findViewById(R.id.btn_tips_agree);
        agreeBtn.setOnClickListener(new ButtonClickListener());
        final Button disagreeBtn = (Button) findViewById(R.id.btn_tips_disagree);
        disagreeBtn.setOnClickListener(new ButtonClickListener());
        mSetBackgroundTask = new SetBackgroundTask();
        mSetBackgroundTask.execute();
    }
    
    private class CheckedChangeListener implements OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            final SharedPreferences prfs = 
                PreferenceManager.getDefaultSharedPreferences(TrafficTipsActivity.this);
            Preferences.StoreTrafficTipsState(prfs.edit(), !isChecked);
        }
    }
    
    private class ButtonClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            final int id = v.getId();
            switch(id) {
                case R.id.btn_tips_agree:
                    startSplashActivity();
                    break;
                case R.id.btn_tips_disagree:
                    TrafficTipsActivity.this.finish();
                    break;
            }
        }
    }
    
    private class SetBackgroundTask extends AsyncTask<Void, Void, Drawable> {

        @Override
        protected void onPreExecute() {
            mBackground = (ImageView) findViewById(R.id.traffic_tips_bg);
        }
        
        @Override
        protected Drawable doInBackground(Void... params) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        protected void onPostExecute(Drawable result) {
            setBackground(result);
        }
    }
    
    private void setBackground(Drawable drawable) {
        final ImageView bg = mBackground;
        if(drawable == null) {
            final Drawable dbg = getResources().getDrawable(R.drawable.flash_default_bg);
            bg.setBackgroundDrawable(dbg);
        } else {
            bg.setBackgroundDrawable(drawable);
        }
    }
}
